package com.rainng.coursesystem.controller;


import com.rainng.coursesystem.model.CourseResourceItem;
import com.rainng.coursesystem.service.CourseResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;

@RequestMapping("/courseresource")
@RestController
public class CourseResourceController extends BaseController {
    @Autowired
    private CourseResourceService courseresourceservice;

    @Value("D:\\2")
    private String uploadDir;

    @PostMapping("/addcourseware")
    public ResponseEntity<String> addCourseware(
            @RequestParam("file") MultipartFile file,
            @RequestParam long courseid) {
        try {
            String message = courseresourceservice.uploadAndAddToCourseResources(file, courseid, "ware");
            return ResponseEntity.ok(message);
        } catch (Exception e) {
            return ResponseEntity.status(500).body("Failed to upload file and add to resources: " + e.getMessage());
        }
    }

    // 添加试卷资料
    @PostMapping("/addtest")
    public ResponseEntity<String> addTestPaper(
            @RequestParam("file") MultipartFile file,
            @RequestParam long courseid) {
        try {
            String message = courseresourceservice.uploadAndAddToCourseResources(file, courseid, "test");
            return ResponseEntity.ok(message);
        } catch (Exception e) {
            return ResponseEntity.status(500).body("Failed to upload file and add to resources: " + e.getMessage());
        }
    }

    // 添加习题资料
    @PostMapping("/addexercise")
    public ResponseEntity<String> addExercise(
            @RequestParam("file") MultipartFile file,
            @RequestParam long courseid) {
        try {
            String message = courseresourceservice.uploadAndAddToCourseResources(file, courseid, "exercise");
            return ResponseEntity.ok(message);
        } catch (Exception e) {
            return ResponseEntity.status(500).body("Failed to upload file and add to resources: " + e.getMessage());
        }
    }

    // 从课程资源中移除文件
    @DeleteMapping("/remove")
    public ResponseEntity<String> removeCourseResource(@RequestParam Long courseid, @RequestParam UUID fileId) {
        try {
            courseresourceservice.removeCourseResource(courseid, fileId);
            return ResponseEntity.ok("File removed from courseresource successfully.");
        } catch (Exception e) {
            return ResponseEntity.status(404).body("File not found in courseresource: " + e.getMessage());
        }
    }

    @GetMapping("/viewware")
    public ResponseEntity<List<CourseResourceItem>> ListCourseWare(@RequestParam Long courseId) {
        List<CourseResourceItem> items = courseresourceservice.listCourseResources(courseId,"ware");
        return ResponseEntity.ok(items);
    }

    @GetMapping("/viewtest")
    public ResponseEntity<List<CourseResourceItem>> ListCourseTest(@RequestParam Long courseId) {
        List<CourseResourceItem> items = courseresourceservice.listCourseResources(courseId,"test");
        return ResponseEntity.ok(items);
    }

    @GetMapping("/viewexercise")
    public ResponseEntity<List<CourseResourceItem>> ListCourseExercise(@RequestParam Long courseId) {
        List<CourseResourceItem> items = courseresourceservice.listCourseResources(courseId,"exercise");
        return ResponseEntity.ok(items);
    }

    @GetMapping("/download")
    public ResponseEntity<Resource> downloadFile(@RequestParam UUID fileId) {
        try {
            CourseResourceItem item = courseresourceservice.getCourseResourceById(fileId);

            if (item != null) {
                Path path = Paths.get(item.getFileData());
                Resource resource = new UrlResource(path.toUri());
                return ResponseEntity.ok()
                        .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + item.getFileName() + "\"")
                        .body(resource);
            } else {
                return ResponseEntity.status(404).body(null);
            }
        } catch (IOException e) {
            return ResponseEntity.status(500).body(null);
        }
    }


}
